<%= render(:partial => "shared/flash_message") %>
<%= render :partial => "shared/config_save_actions" -%>

<% scm_config = scope[:material].getSCMConfig() %>
<%= form_for(scope[:material], :as => :material, :url => scope[:url], :html => {"novalidate"=> "novalidate", :id=> "material_form", :name=> "material_form", :method => scope[:method], :onsubmit => "return AjaxForm.jquery_ajax_submit(this);", :class => "popup_form"}) do |f| %>
    <% form_name_provider_obj = form_name_provider(f) %>
    <% plugged_material_angular_controller_element_id = form_name_provider_obj.css_id_for("angular_" + scm_config.getSCMType()) %>
    <% plugged_material_data_element_id = form_name_provider_obj.css_id_for("data_" + scm_config.getSCMType()) %>

    <%= md5_field %>

    <%= f.hidden_field com.thoughtworks.go.config.materials.PluggableSCMMaterialConfig::SCM_ID -%>

    <div class="form_content">
        <%= render :partial => "admin/shared/global_errors.html", :locals => {:scope => {}} -%>
        <div id="material">
            <div class="fieldset">
                <div id="<%= plugged_material_angular_controller_element_id %>" name="<%= plugged_material_angular_controller_element_id %>" class="plugged_material">
                    <div class="form_item">
                        <div class="form_item_block required">
                            <%= required_label(f, com.thoughtworks.go.domain.scm.SCM::NAME, 'Material Name') %>
                            <%= f.text_field com.thoughtworks.go.domain.scm.SCM::NAME, {:class => "form_input", :id => nil} -%>
                            <%= error_message_on(scm_config, com.thoughtworks.go.domain.scm.SCM::NAME, :css_class => "form_error") %>
                        </div>

                        <div class="plugged_material_template required">
                            <%== scope[:meta_data_store].template(scope[:material].getPluginId()) %>
                        </div>

                        <div class="form_item_block with-padding-top">
                            <%= submit_button('CHECK CONNECTION', :type => "button", :id => "check_connection_pluggable_scm") -%>
                            <span id="pluggable_scm_check_connection_message"></span>
                        </div>

                        <span id="<%= plugged_material_data_element_id %>" class="plugged_material_data" style="display: none">
                            <%= com.google.gson.Gson.new.toJson(scm_config.getConfigAsMap()) %>
                        </span>

                        <div class="form_item_block checkbox_row material_options">
                            <%= f.check_box(com.thoughtworks.go.domain.scm.SCM::AUTO_UPDATE, {:class => "form_input"}, "true") -%>
                            <%= f.label(com.thoughtworks.go.domain.scm.SCM::AUTO_UPDATE, 'Poll for new changes') %>
                            <%= error_message_on(scm_config, com.thoughtworks.go.domain.scm.SCM::AUTO_UPDATE, :css_class => "form_error") %>
                        </div>

                        <div class="form_item_block">
                            <label>Destination Directory:</label>
                            <%= f.text_field(com.thoughtworks.go.config.materials.PluggableSCMMaterialConfig::FOLDER, {:class => "form_input", :id => nil}) -%>
                            <%= error_message_on(scope[:material], com.thoughtworks.go.config.materials.PluggableSCMMaterialConfig::FOLDER, :css_class => "form_error") %>
                        </div>
                    </div>
                    <%= render :partial => 'shared/form_required_message.html', :locals => {:scope => {}} %>
                </div>
            </div>


            <h3>Blacklist</h3>
            <div class="fieldset">
                <label>Enter the paths to be excluded. Separate multiple entries with a comma.</label>
                <% scope[:material].filter().each do |ignored_file|
                  error = ignored_file.errors().on(com.thoughtworks.go.config.materials.IgnoredFiles::PATTERN)
                  if error
                    scope[:material].errors().add(com.thoughtworks.go.config.materials.PluggableSCMMaterialConfig::FILTER, error)
                  end
                end %>

                <%= f.text_area(com.thoughtworks.go.config.materials.PluggableSCMMaterialConfig::FILTER, {:class => "form_input", :id => nil, :rows => 2}) -%>
                <%= error_message_on(scope[:material], com.thoughtworks.go.config.materials.PluggableSCMMaterialConfig::FILTER, :css_class => "form_error") %>
                <div class="clear"></div>
            </div>
        </div>
    </div>
  <% if scope[:read_only_page] %>
    <div class="primary finish submit" style="display: none"></div>
  <% else %>
    <%= render :partial => "shared/form_buttons.html", :locals => {:scope => {:submit_label => 'SAVE'}} %>
  <% end %>
    <%= render :partial => 'shared/convert_tool_tips.html' %>

    <script type="text/javascript">
        jQuery(document).ready(function () {
            var form_name_prefix = "<%= form_name_provider_obj.form_name_prefix %>";
            var plugged_material_angular_controller_element_id = "<%= plugged_material_angular_controller_element_id %>";
            var plugged_material_data_element_id = "<%= plugged_material_data_element_id %>";
            new TaskPluginView().initialize(plugged_material_angular_controller_element_id, plugged_material_data_element_id, form_name_prefix);

            new TaskPluginView().bootstrapAngular();

            new PluggableSCMCheckConnection("<%= admin_pluggable_scm_check_connection_path(:plugin_id => scope[:material].getPluginId()) %>").bind("#material_form", "#check_connection_pluggable_scm", "#pluggable_scm_check_connection_message");
        });
    </script>
<% end %>
